﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using XinYi.CrookedShow.BLL;
using XinYi.CrookedShowBack.Models;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShowBack.Controllers
{
    public class ProductsController : Controller
    {
        Product_SpuService product_SpuService = new Product_SpuService();
        CategoryService categoryService = new CategoryService(); 
        #region 查询分页
        public JsonResult GetProductsPageList(int page, int limit)
        {
          
            PageListResult<ProductsInfo> pageListResult = new PageListResult<ProductsInfo>();
            List<ProductsInfo> roleUserQueries = product_SpuService.GetRoleList();           
            pageListResult.data = roleUserQueries.Skip((page - 1) * limit).Take(limit).ToList();
            pageListResult.msg = string.Empty;
            pageListResult.code = 0;
            pageListResult.count = roleUserQueries.Count;
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 判断是否存在
        public JsonResult SelectCategoryName(string name)
        {
            Operate operate = new Operate();
            Expression<Func<Category, bool>> lambdaExpression = a => a.Name == name;
            operate.Success = categoryService.Exists(lambdaExpression);
            return Json(operate);
        }
        #endregion

        #region 添加修改
        public JsonResult AddingInformation(Category category)
        {
            Operate operate = new Operate();
            category.ModifiedTime = DateTime.Now;       //最后修改时间
            if (category.Id != 0)
            {
                operate.Success = categoryService.Update(category);
            }
            else
            {
                
                operate.Success = categoryService.Add(category);
            }
            return Json(operate);
        }
        #endregion

        #region 一级分类,二级分类，三级分类
        public JsonResult GetLeveCategoryList(int leve)
        {
            List<Category> categories = new List<Category>(); ;
            if (leve == 1)
            {
                categories = categoryService.QueryList(a => a.ParentId == 0 && a.CategoryLevel == leve).OrderBy(a => a.Id).ToList();

            }
            else if (leve == 2)
            {
                categories = categoryService.QueryList(a => a.CategoryLevel == leve).OrderBy(a => a.Id).ToList();
            }
            else if (leve == 3)
            {
                categories = categoryService.QueryList(a => a.CategoryLevel == leve).OrderBy(a => a.Id).ToList();
            }
            return Json(categories);
        }
        #endregion

        #region 添加商品
        public JsonResult AddProduct_SpuInfo(AddProduct_Spu addProduct)
        {
            Operate operate = new Operate();
            operate.Success = product_SpuService.AddProduct_SpuInfo(addProduct);
            return Json(operate);
        }
        #endregion
    }
}